home *** CD-ROM | disk | FTP | other *** search
/ Aminet 51 / Aminet 51 (2002)(GTI - Schatztruhe)[!][Oct 2002].iso / Aminet / util / misc / MultiRen.lha / MultiRen / Developers / SwapName-Plugin.mrp.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-24  |  5.7 KB  |  198 lines

  1. /* Swapname-plugin v1.1a rev.#2 by Deniil 715! for MultiRen
  2.    Made 2002-05-24
  3.  
  4.    My e-mail: Daniel Westerberg: deniil@algonet.se
  5. */
  6.  
  7. #include<proto/exec.h>
  8. #include<proto/dos.h>
  9. #include<proto/intuition.h>
  10. #include<exec/tasks.h>
  11. #include<intuition/intuition.h>
  12. #include<stdlib.h>
  13. #include<stdio.h>
  14. #include<string.h>
  15.  
  16. /* This is how many strings this plugin will return. */
  17. #define NUMSTRINGS_FOR_THIS_PLUGIN 1
  18.  
  19. /* Private, don't touch! */
  20. #define FILE_NAME_LENGTH 512
  21. #define COM_ASK 1
  22. #define COM_EXTRACT 2
  23. #define COM_CONFIGURE 3
  24. #define COM_ABOUT 4
  25. #define COM_QUIT 5
  26.  
  27. /* Use these as return values wherever suitable.
  28. Return ERR_OK if all went well and the string are filled.
  29. */
  30. #define ERR_OK 0
  31. #define ERR_NOMEM 1
  32. #define ERR_NOFILE 2
  33. #define ERR_NOSIG 3
  34. #define ERR_NOTIMPL 4
  35. #define ERR_UNKNOWN 5
  36. #define ERR_OTHER 6
  37. #define ERR_WRONGFORMAT 7
  38. #define ERR_NOINFO 8
  39. #define ERR_FATAL 20
  40.  
  41. struct multiren_plugin {
  42.   long id;
  43.   Task *task;
  44.   long sig;
  45.   short ret;
  46.   char command;
  47.   char numstrings;       /* You will */
  48.   char *stringlist[256]; /* only use */
  49.   char *name;            /* these 4  */
  50.   char newname;          /* elements */
  51. };
  52.  
  53. const char VersionString[]="$VER: Swapname-plugin v1.1a by Deniil 715! rev.#2 (2002-05-24)"
  54.  
  55. short ask(void);
  56. short extract(void);
  57. short configure(void);
  58. short about(void);
  59. short cleanup(void);
  60. short req(char*,char*);
  61.  
  62. struct multiren_plugin *mrp;
  63. char  s[FILE_NAME_LENGTH+1];
  64. short inclext=1;
  65. BPTR  fh;
  66.  
  67. short main(int argc,char *argv[]) {
  68.   char sigbit;
  69.   long sig, msig;
  70.   Task *mtask;
  71.   if(DOSBase=(struct DosLibrary*)OpenLibrary("dos.library",0)) {
  72.     if(IntuitionBase=(struct IntuitionBase*)OpenLibrary("intuition.library",0)) {
  73.       if(argc==2) {
  74.         if(mrp=(struct multiren_plugin*)atol(argv[1])) {
  75.           if(mrp->id==*(long*)"MRPO") {
  76.             if((sigbit=AllocSignal(-1))>=0) {
  77.               sig=1<<sigbit;
  78.               while(1) {
  79.                 switch(mrp->command) {
  80.                 case COM_ASK:
  81.                   mtask=mrp->task;
  82.                   msig=mrp->sig;
  83.                   mrp->ret=ask();
  84.                   SetProgramName(mrp->name);
  85.                   mrp->task=FindTask(0L);
  86.                   mrp->sig=sig;
  87.                   break;
  88.                 case COM_EXTRACT:   mrp->ret=extract(); break;
  89.                 case COM_CONFIGURE: mrp->ret=configure(); break;
  90.                 case COM_ABOUT:     mrp->ret=about(); break;
  91.                 case COM_QUIT:
  92.                   mrp->ret=cleanup();
  93.                   FreeSignal(sigbit);
  94.                   CloseLibrary((struct Library*)DOSBase);
  95.                   Signal(mtask,msig);
  96.                   return 0;
  97.                 default:
  98.                   mrp->ret=ERR_UNKNOWN;
  99.                 }
  100.                 Signal(mtask,msig);
  101.                 Wait(sig);
  102.               }
  103.             }
  104.             else {
  105.               mrp->ret=ERR_NOSIG;
  106.               Signal(mrp->task,mrp->sig);
  107.               return 0;
  108.             }
  109.           }
  110.         }
  111.       }
  112.       CloseLibrary((struct Library*)IntuitionBase);
  113.     }
  114.     CloseLibrary((struct Library*)DOSBase);
  115.   }
  116.   printf("This is a plugin for MultiRen!\n"
  117.          "It is not supposed to be executed manually!\n"
  118.          "Use it through the Renplacer tool in MultiRen instead!\n");
  119.   return ERR_FATAL;
  120. }
  121.  
  122. short ask() {
  123.   mrp->numstrings=NUMSTRINGS_FOR_THIS_PLUGIN;     /* I will return one string */
  124.   mrp->stringlist[0]="Filename turned backwards"; /* Setting information-strings */
  125.   mrp->name="SwapName"; /* This plugins name */
  126.   mrp->newname=1;       /* As this plugin is doing stuff with the filename
  127.                            and not the file it is wise to request New name. */
  128.   if(fh=Open("PROGDIR:Plugins/SwapName-plugin.cfg",MODE_OLDFILE)) { /* Open configfile */
  129.     inclext=FGetC(fh); /* The prefs telling if we should include
  130.                           extension in the swap or not.. */
  131.     Close(fh);
  132.   }
  133.   return ERR_OK;
  134. }
  135.  
  136. short extract() { /* Do my stuff.. */
  137.   int  x, y, z;
  138.   char *st;
  139.   st=FilePart(mrp->name); /* Note that we get the complete path in name
  140.                              and must separate it to get the filename. */
  141.   x=strlen(st);
  142.   if(x<FILE_NAME_LENGTH) {
  143.     if(inclext) {
  144.       for(y=0;y<x;y++) s[y]=st[x-1-y];
  145.       s[x]='\0';
  146.     }
  147.     else {
  148.       for(z=x;z>=0;z--) if(st[z]=='.') break;
  149.       if(z>0) z--; else z=x;
  150.       for(y=0;y<=z;y++) s[y]=st[z-y];
  151.       s[z+1]='\0';
  152.       if(z<x) strcat(s,st+z+1);
  153.     }
  154.     mrp->stringlist[0]=s; /* Setting the string-list position 0 to point to our string */
  155.     return ERR_OK;
  156.   }
  157.   else
  158.     return ERR_OTHER; /* Filename was longer than the set max-length for MultiRen v1.3 */
  159. }
  160.  
  161. short configure() {
  162.   char *str;
  163.   if(inclext) str="Yes"; else str="No";
  164.   sprintf(s,"Do you want the extension to be\n"
  165.             "part of the filename swapping??\n"
  166.             "Current state: %s",str);
  167.   inclext=req(s,"Yes|No|Cancel");
  168.   if(inclext) {
  169.     if(fh=Open("PROGDIR:Plugins/SwapName-plugin.cfg",MODE_NEWFILE)) {
  170.       FPutC(fh,inclext & 1);
  171.       Close(fh);
  172.     }
  173.     else
  174.       req("Could not save the settings!","OK");
  175.   }
  176.   return ERR_OK; /* Will always return ERR_OK here so that MultiRen will not
  177.                     put up another requester telling this operation failed. */
  178. }
  179.  
  180. short about() {
  181.   if(req("SwapName Plugin v1.1a rev.#2 by Deniil 715! for MultiRen\n\n"
  182.          "It was made 2002-05-24 in Amiga-E\n\n"
  183.          "E-mail: deniil@algonet.se","More|OK"))
  184.     req("This plugin will swap the filename backwards.\n"
  185.         "It features a setting to include the file-\n"
  186.         "extension in the swapping or not.","OK");
  187.   return ERR_OK;
  188. }
  189.  
  190. short cleanup() { return ERR_OK; }
  191.  
  192. short req(char *body,char *gads) {
  193.   struct EasyStruct tags;
  194.   tags.es_Title="Test Plugin";
  195.   tags.es_TextFormat=body;
  196.   tags.es_GadgetFormat=gads;
  197.   return EasyRequestArgs(NULL,&tags,NULL,NULL);
  198. }